#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <dirent.h>
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>

#include "../my_inc/file_manager.h"
#include "../my_inc/user_manager.h"
#include "../mainUI/ui.h"
#include "../my_inc/canvas.h"

// 文件浏览界面下，“退出”按钮事件
void ui_event_btn_exit(lv_event_t * e)
{
    lv_event_code_t event_code = lv_event_get_code(e);

    if(event_code == LV_EVENT_CLICKED) 
    {
        // 返回主界面
        _ui_screen_change(&ui_MainUI, LV_SCR_LOAD_ANIM_FADE_ON, 500, 0, &ui_MainUI_screen_init);
        P_DI p_di = (struct tmp_inf *)e->user_data;
        free(p_di);
        printf("free succeed!\n");
    }
}


// 文件浏览主界面初始化
P_DI Demo_Init()
{
    P_DI p_di = (P_DI)malloc(sizeof(DI));
    if(p_di == (P_DI)NULL)
    {
        perror("malloc ...");
        return (P_DI)-1;
    }

    memset(p_di,0,sizeof(DI));

    p_di->main_windows   = lv_obj_create(NULL);

    p_di->dir_list       = lv_list_create(p_di->main_windows);
    
    p_di->little_windows = lv_obj_create(p_di->main_windows);
    

    lv_obj_set_size(p_di->dir_list,300,480);//设置左边目录列表大小
    lv_obj_set_size(p_di->little_windows,480,480);
    lv_obj_set_pos(p_di->dir_list,0,10);
    lv_obj_set_pos(p_di->little_windows,310,30);

    lv_scr_load(p_di->main_windows); 

    /* 退出按钮 */
    lv_obj_t *btn_exit = lv_btn_create(p_di->main_windows);
    lv_obj_set_size(btn_exit, 60, 30);
    lv_obj_align(btn_exit, LV_ALIGN_TOP_RIGHT, 0, 0);
    // lv_obj_t *lbl_exit = lv_label_create(btn_exit);
    // lv_label_set_text(lbl_exit, "Exit");
    lv_obj_t *icon = lv_img_create(btn_exit);
    lv_img_set_src(icon, LV_SYMBOL_CLOSE); 

    lv_obj_add_event_cb(btn_exit, ui_event_btn_exit, LV_EVENT_ALL, p_di);
    
    return p_di;
}

// 点击目录按钮事件
void Press_Dir_Btn(lv_event_t *event)
{   
    char base_dir_path[256]= "\0";
    P_DI p_di = ((struct tmp_inf *)(event->user_data))->p_di;
    strcpy(base_dir_path,((struct tmp_inf *)(event->user_data))->base_dir_path);
    
    free(((struct tmp_inf *)(event->user_data)));

    printf("%s\n",base_dir_path);
    


    lv_obj_del(p_di->little_windows); 
    lv_obj_del(lv_obj_get_parent(event->current_target)); 
    printf("+++++----\n");
    // 处理按钮点击事件和关联的数据
    // ...
    p_di->dir_list      = lv_list_create(p_di->main_windows);
    p_di->little_windows = lv_obj_create(p_di->main_windows);
    lv_obj_set_size(p_di->dir_list,300,480);//设置大小
    lv_obj_set_size(p_di->little_windows,480,480);
    lv_obj_set_pos(p_di->dir_list,0,10);
    lv_obj_set_pos(p_di->little_windows,310,20);
    Show_File_List(base_dir_path,p_di);

    return ;
}

// 点击图片按钮事件
void Press_Pic_Btn(lv_event_t *event)
{
    P_DI p_di = ((struct tmp_inf *)(event->user_data))->p_di;

    //删掉主界面

    lv_obj_del(p_di->main_windows);
    
    free(p_di);


    lv_obj_t * main_draw   = lv_obj_create(NULL);

    lv_obj_t * main_windows   = lv_win_create(main_draw,30);
    //lv_obj_set_size(main_windows, LV_HOR_RES, LV_VER_RES);
    //lv_obj_align(main_windows, LV_ALIGN_CENTER, 0, 0); // 将窗口居中
    
    lv_obj_t * close_btn      = lv_win_add_btn(main_windows,LV_SYMBOL_CLOSE,30);
   
    lv_obj_set_pos(close_btn,450,10);
    
    lv_obj_add_event_cb(close_btn,Close_Img_Btn,LV_EVENT_SHORT_CLICKED,main_draw);

    char obj_img_path[256] = "\0";
    sprintf(obj_img_path,"%s%s","S:",((struct tmp_inf *)(event->user_data))->base_dir_path);
    char * obj_p = &obj_img_path[strlen(obj_img_path)-4];
    //释放ti
    free(((struct tmp_inf *)(event->user_data)));

    if(strcmp(obj_p,".gif") == 0)
    {
        // 创建一个图片对象用于显示 GIF 动画
        lv_obj_t *gif_img = lv_gif_create(main_draw);
    
        // 设置 GIF 图片对象的大小模式为正常大小
        lv_img_set_size_mode(gif_img, LV_IMG_SIZE_MODE_REAL);
        // 将 GIF 图片对象居中
        lv_obj_align(gif_img, LV_ALIGN_CENTER, 0, 30);
        lv_gif_set_src(gif_img, obj_img_path); // 设置 GIF 图片路径
    }
    else
    {
        lv_obj_t * img = lv_img_create(main_draw);  // 在窗口上创建一个图像对象
        lv_obj_align(img,LV_ALIGN_CENTER, 0, 30); //设置居中
        lv_img_set_size_mode(img,LV_IMG_SIZE_MODE_REAL);
        
        printf("+++%s+++\n",obj_img_path);
        lv_img_set_src(img,obj_img_path);  // 设置图像的路径
    }
    lv_scr_load(main_draw);
    return;
}

//图片浏览界面下，点击关闭按钮事件
void Close_Img_Btn(lv_event_t *event)
{
    //删掉图片界面
    lv_obj_del((lv_obj_t *)event->user_data);

    //重新启动主界面
    P_DI p_di = Demo_Init();
    Show_File_List(user_path,p_di);

    return ;
}


// void Press_Pic_Btn(lv_event_t *event)
// {
//     if(canvas == NULL) 
//     {
//         printf("请先新建一个画布！\n");
//         _ui_screen_change(&ui_MainUI, LV_SCR_LOAD_ANIM_FADE_ON, 500, 0, &ui_MainUI_screen_init);
//     }
//     struct tmp_inf *ti = ((struct tmp_inf *)event->user_data)->p_di;
//     // P_DI p_di         = ti->p_di;

//     /* 1. 构造绝对路径 */
//     // char path[256];
//     // snprintf(path, sizeof(path), "S:%s", ti->base_dir_path);
//     char obj_img_path[256] = "\0";
//     sprintf(obj_img_path,"%s%s","S:",((struct tmp_inf *)(event->user_data))->base_dir_path);
//     printf("当前路径：%s\n", obj_img_path);
    
//     /* 3. 回到画布界面 */
//     _ui_screen_change(&ui_MainUI, LV_SCR_LOAD_ANIM_FADE_ON, 500, 0, &ui_MainUI_screen_init);
//     create_canvas(ui_Panel1);
//     clear_canvas();

//     /* 4. 加载图片到画布 */ 
//     load_bmp_to_canvas(obj_img_path);

//     // draw_png_to_canvas(canvas, obj_img_path);

//     printf("test\n----------------------------\n");

//     free(ti);
// }



// 浏览指定文件夹
int Show_File_List(const char * obj_dir_path,P_DI p_di)
{
    DIR * dp = opendir(obj_dir_path);
    if(dp == (DIR *)NULL)
    {
        perror("opendir ... ");
        return -1;
    }

    char base_path[256];

    int x=0,y=0;
    while(1)
    {
        memset(base_path,0,256);
        
        struct dirent * eq = readdir(dp);
        if(eq == NULL)
        {
            break;
        }
        
        if(obj_dir_path[strlen(obj_dir_path)-1] == '/')
        {
            sprintf(base_path,"%s%s",obj_dir_path,eq->d_name);
        }
        else
        {
            sprintf(base_path,"%s/%s",obj_dir_path,eq->d_name);
        }
        
        struct tmp_inf * ti = (struct tmp_inf *)malloc(sizeof(struct tmp_inf));
        memset(ti,0,sizeof(struct tmp_inf));
        
        strcpy(ti->base_dir_path,base_path);
        
        ti->p_di          = p_di;
        
        if(eq->d_type == DT_DIR)
        {
            
            lv_obj_t * dir_btn_1 = lv_list_add_btn(p_di->dir_list,LV_SYMBOL_DIRECTORY,eq->d_name);
            lv_obj_add_event_cb(dir_btn_1,Press_Dir_Btn,LV_EVENT_SHORT_CLICKED,ti);
            
        }
        else
        {
            // 左侧列表文件显示
            lv_obj_t * dir_btn_1 = lv_list_add_btn(p_di->dir_list, LV_SYMBOL_FILE, eq->d_name);
            // 右侧小窗文件显示
            lv_obj_t * dir_btn_2 = lv_btn_create(p_di->little_windows);
            lv_obj_set_size(dir_btn_2,50,50);
            lv_obj_set_pos(dir_btn_2,x,y);

            lv_obj_t *icon = lv_img_create(dir_btn_2);
            lv_img_set_src(icon, LV_SYMBOL_FILE);              // 目录 or 文件图标

            lv_obj_t *label = lv_label_create(p_di->little_windows);
            lv_label_set_text(label,eq->d_name); // 设置标签的文本内容
            lv_obj_set_pos(label,x,y+60);

            x+=160;
            if((480-x) <= 70)
            {
                x = 0;
                y+= 100;
            }

            char * obj_p = &eq->d_name[strlen(eq->d_name)-4];

            if((strcmp(obj_p,".bmp") == 0) || (strcmp(obj_p,".gif") == 0) || (strcmp(obj_p,".jpg") == 0) || (strcmp(obj_p,".png") == 0))
            {
                lv_obj_add_event_cb(dir_btn_2,Press_Pic_Btn,LV_EVENT_CLICKED,ti);
            }
            else{
                //没有注册事件处理函数,直接释放空间
                free(ti);
            }
        }
        

    }

    closedir(dp);
    return 0;
}